﻿using System;
using System.Collections.Generic;
using System.Text;
using YouHP.Utility;
using YouHP.BLL;
using System.Web;
using YouHP.Model;
using YouHP.Logic;

namespace YouHP.Pages.Ajax
{
    public class EmotionUpdateAjax : YouHP.Pages.BasePage
    {

        protected int toUserId;
        protected string receivers;
        protected StringBuilder sb = new StringBuilder();
        protected EmotionLogic emotionLogic = new EmotionLogic();

        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            if (UserData == null)
                return;

            int userId = UserData.Id;
            PatientRecordInfo prData = PatientBLL.Instance.GetPatientRecordInfo(userId);
            if (prData == null || prData.Id <= 0)
            {
                sb.Append("不存在用户记录!");
                ResponseXML(sb);
                return;
            }

            string statusDesc = RequestTools.GetString("statusdesc");
            if (statusDesc == "")
                statusDesc = "没什么好说的...";

            int emotionTypeIdx = RequestTools.GetInt("emotiontypeidx", -1);
            if (emotionTypeIdx < 0)
            {
                sb.Append("错误的心情类型!");
                ResponseXML(sb);
                return;
            }

            List<EmotionTypeInfo> allEmotionTypes = emotionLogic.GetAllEmotionTypes();
            if (emotionTypeIdx > allEmotionTypes.Count-1)
            {
                sb.Append("错误的心情类型!");
                ResponseXML(sb);
                return;
            }

            EmotionInfo emotionInfo = new EmotionInfo();
            emotionInfo.UserId = userId;
            emotionInfo.PrId = prData.Id;
            emotionInfo.EmotionType = allEmotionTypes[emotionTypeIdx].TypeId;
            emotionInfo.StatusDescription = statusDesc;
            int id = emotionLogic.AddEmotion(emotionInfo);

            if (id > 0)
            {
                EmotionTypeInfo typeInfo = emotionLogic.GetEmotionType(emotionInfo.EmotionType);
                sb.Append("<img src=\"" + typeInfo.Img + "\" style=\"width:30px; height:30px\" />");
                sb.Append("<font style=\"font-size:13px;\">" + statusDesc + "</font>");

                //添加动态
                UserActionInfo info = new UserActionInfo();
                info.UserId = UserData.Id;
                info.UserName = UserData.UserName;
                info.ActionType = Constants.ACTION_EMOTION;
                info.ActionContent = "更新心情为：<img src=\"" + typeInfo.Img + "\" style=\"width:30px; height:30px\" /><font style=\"font-size:13px;\">" + statusDesc + "</font>";
                info.ActionTime = DateTime.Now;
                UserActionBLL.Instance.AddUserAction(info);

            }
            else
                sb.Append("失败!");

            ResponseXML(sb);
        }

        protected void ResponseXML(StringBuilder sb)
        {
            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.ContentType = "Text/html";
            System.Web.HttpContext.Current.Response.Expires = 0;

            System.Web.HttpContext.Current.Response.Write(sb.ToString());
            System.Web.HttpContext.Current.Response.End();
        }

    }
}
